#include "head.h"



int main(void)
{
    // 建立连接
    int sock_fd;
    tcp(&sock_fd);

    me_t me;
    messenger_t msg;


    bzero(&me, sizeof(me));
    while(1)
    {   
        if(me.login == false)
        {
            printf("\r****************\n");
            printf("\r*    1、登陆    *\n");
            printf("\r*    2、注册    *\n");
            printf("\r*    3、退出    *\n");
            printf("\r****************\n");
            fprintf(stderr, "\r你的选择是: ");
            
            int option;
            scanf("%d", &option);

            char c;
            while(((c = getchar()) != '\n') && c != EOF);

            switch(option)
            {
            case 1: login(&me, sock_fd);    break;
            case 2: register_(&me, sock_fd);    break;
            case 3: return 0;
            default: break;
            }
        }
        else if(me.login == true)
        {   
            // printf("me.token = %s\n", me.token);
            fprintf(stderr, "\r\033[34m%s\033[0m: %s$ ",me.name, me.path);
            
            char cmd[100] = {0};
            fgets(cmd, sizeof(cmd), stdin);
            // char c;
            // while(((c = getchar()) != '\n') && c != EOF);
            // 分析命令
            cmd_t ret = analyse(cmd);
            /// 还需要修改 判断参数个数
            // printf("cmd = %s\n", cmd);
            
            switch(ret)
            {
            // case LS: do_ls(&me, sock_fd);  break;
            case PWD: pwd(&me);  break;
            case MKDIR: mkdir_(&me, sock_fd, cmd);
            case CD: cd(&me, sock_fd, cmd);  break;
            case ERROR: printf("错误的指令\n"); break;
            case LS: ls(&me, sock_fd, cmd); break;
            case UP:
                bzero(&msg, sizeof(msg));
                make_protocal(&msg, ret, &me, cmd);
                printf("1111 args = %s\n", msg.args);
                pthread_t pid = 0;
                pthread_create(&pid, NULL, start_routine, &msg); break;
            }

        }

    }
    
    close(sock_fd);
    return 0;
}